﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media.Animation;

namespace Gmantis.Controls.Toolbar
{
    [TemplatePart(Name = "CollapseButton", Type = typeof(ToggleButton)), TemplatePart(Name = "HelpButton", Type = typeof(Button))]
    public class ToolbarTabControl : TabControl
    {
        // Fields
        internal ToggleButton _elementCollapseButton;
        internal Button _elementHelpButton;
        internal bool _isLoaded;
        internal const string CollapseButtonElementName = "CollapseButton";
        internal const string HelpButtonElementName = "HelpButton";
        private bool notify = true;

        // Methods
        public ToolbarTabControl()
        {
            base.DefaultStyleKey = typeof(ToolbarTabControl);
            this.SetCustomDefaultValues();
        }

        private void Collapse()
        {
            this.notify = false;
            base.SelectedItem = null;
            Border templateChild = base.GetTemplateChild("SelectedItemContentBorder") as Border;
            if (templateChild != null)
            {
                templateChild.Visibility = Visibility.Collapsed;
            }
            if (this._elementCollapseButton != null)
            {
                this._elementCollapseButton.IsChecked = true;
            }
            this.notify = true;
        }

        private void Expand()
        {
            this.notify = false;
            if (base.SelectedItem == null)
            {
                base.SelectedIndex = 0;
            }
            Border templateChild = base.GetTemplateChild("SelectedItemContentBorder") as Border;
            if (templateChild != null)
            {
                templateChild.Visibility = Visibility.Visible;
            }
            if (this._elementCollapseButton != null)
            {
                this._elementCollapseButton.IsChecked = false;
            }
            this.notify = true;
        }

        private T GetTemplateChild<T>(string childName, bool required, ref string errors) where T : class
        {
            DependencyObject templateChild = base.GetTemplateChild(childName);
            ApplyTemplateHelper.VerifyTemplateChild(typeof(T), "template part", childName, templateChild, required, ref errors);
            return (templateChild as T);
        }

        private static Storyboard GetTemplateChildResource(FrameworkElement root, string resourceName, bool required, ref string errors)
        {
            return GetTemplateChildResource<Storyboard>(root, resourceName, required, ref errors);
        }

        private static T GetTemplateChildResource<T>(FrameworkElement root, string resourceName, bool required, ref string errors) where T : class
        {
            object child = root.Resources[resourceName];
            ApplyTemplateHelper.VerifyTemplateChild(typeof(T), "resource", resourceName, child, required, ref errors);
            return (child as T);
        }

        private void InitializeCollapseButtonPart()
        {
            if (this.Toolbar != null)
            {
                this._elementCollapseButton.Visibility = this.Toolbar.ShowCollapseButton ? Visibility.Visible : Visibility.Collapsed;
            }
            this._elementCollapseButton.Click += delegate(object s, RoutedEventArgs e)
            {
                if (this._elementCollapseButton.IsChecked == true)
                {
                    this.Collapse();
                }
                else
                {
                    this.Expand();
                }
            };
        }

        private void InitializeHelpButtonPart()
        {
            if (this.Toolbar != null)
            {
                this._elementHelpButton.Visibility = this.Toolbar.ShowHelpButton ? Visibility.Visible : Visibility.Collapsed;
            }
            this._elementHelpButton.Click += delegate(object s, RoutedEventArgs e)
            {
                if (this.Toolbar != null)
                {
                    this.Toolbar.OnHelpClick();
                }
                else
                {
                    Toolbar parent = base.Parent as Toolbar;
                    if (parent != null)
                    {
                        parent.OnHelpClick();
                    }
                }
            };
        }

        public override void OnApplyTemplate()
        {
            string errors = string.Empty;
            base.OnApplyTemplate();
            this._isLoaded = true;
            this._elementHelpButton = this.GetTemplateChild<Button>("HelpButton", true, ref errors);
            if (this._elementHelpButton != null)
            {
                this.InitializeHelpButtonPart();
            }
            this._elementCollapseButton = this.GetTemplateChild<ToggleButton>("CollapseButton", true, ref errors);
            if (this._elementCollapseButton != null)
            {
                this.InitializeCollapseButtonPart();
            }
            if (!string.IsNullOrEmpty(errors))
            {
                this._isLoaded = false;
                if ((base.Template != null) && !PlatformIndependent.IsInDesignMode(this))
                {
                    throw new InvalidOperationException(string.Format(CultureInfo.InvariantCulture, "Template cannot be applied to ToolbarTabControl.\nDetails: {0}", new object[] { errors }));
                }
            }
        }

        private void SetCustomDefaultValues()
        {
            base.SelectionChanged += delegate(object s, SelectionChangedEventArgs e)
            {
                if (this.notify)
                {
                    if (base.IsEnabled && base.IsMouseOver)
                    {
                        this.Expand();
                    }
                    if ((this._elementCollapseButton != null) && (this._elementCollapseButton.IsChecked == true))
                    {
                        base.SelectedItem = null;
                    }
                }
            };
        }

        // Properties
        internal Toolbar Toolbar { get; set; }
    }
}
